package cc.rengu.igas.momp.biz.service.impl;

import cc.rengu.igas.momp.common.enums.MompTxnNumEnum;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.PublishMcService;
import cc.rengu.igas.momp.facade.request.*;
import cc.rengu.igas.momp.facade.response.*;
import cc.rengu.igas.momp.facade.result.Result;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;

/**
 * 发布营销活动接口实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/3/24 11:55
 */
@SuppressWarnings("unchecked")
public class PublishMcServiceImpl implements PublishMcService {

    @Override
    public Result<PublishAgentProfitSharingMcResponse> publishAgentProfitSharingMc(PublishAgentProfitSharingMcRequest publishAgentProfitSharingMcRequest) {

        DubboService<PublishAgentProfitSharingMcRequest> dubboService = new DubboServiceImpl();
        Result<PublishAgentProfitSharingMcResponse> publishAgentProfitSharingMcResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.PUBLISH_APS_MC.getTxnNum().equals(publishAgentProfitSharingMcRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<PublishAgentProfitSharingMcResponse> dubboResult = dubboService.callAtomService(publishAgentProfitSharingMcRequest,
                    MompTxnNumEnum.PUBLISH_APS_MC.getService(), PublishAgentProfitSharingMcResponse.class);
            publishAgentProfitSharingMcResponseResult.setResult(dubboResult.getResult());
            publishAgentProfitSharingMcResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            PublishAgentProfitSharingMcResponse publishAgentProfitSharingMcResponse = new PublishAgentProfitSharingMcResponse();
            publishAgentProfitSharingMcResponseResult.setResult(publishAgentProfitSharingMcResponse);
            publishAgentProfitSharingMcResponse.setHeader(publishAgentProfitSharingMcRequest.getHeader());
            publishAgentProfitSharingMcResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                publishAgentProfitSharingMcResponse.setRespCode(((BizException) e).getCode());
                publishAgentProfitSharingMcResponse.setRespDesc(e.getMessage());
            } else {
                publishAgentProfitSharingMcResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                publishAgentProfitSharingMcResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return publishAgentProfitSharingMcResponseResult;
    }

    @Override
    public Result<PublishCashierCashbackMcResponse> publishCashierCashbackMc(PublishCashierCashbackMcRequest publishCashierCashbackMcRequest) {

        DubboService<PublishCashierCashbackMcRequest> dubboService = new DubboServiceImpl();
        Result<PublishCashierCashbackMcResponse> publishCashierCashbackMcResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.PUBLISH_CCB_MC_INFO.getTxnNum().equals(publishCashierCashbackMcRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<PublishCashierCashbackMcResponse> dubboResult = dubboService.callAtomService(publishCashierCashbackMcRequest,
                    MompTxnNumEnum.PUBLISH_CCB_MC_INFO.getService(), PublishCashierCashbackMcResponse.class);
            publishCashierCashbackMcResponseResult.setResult(dubboResult.getResult());
            publishCashierCashbackMcResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            PublishCashierCashbackMcResponse publishCashierCashbackMcResponse = new PublishCashierCashbackMcResponse();
            publishCashierCashbackMcResponseResult.setResult(publishCashierCashbackMcResponse);
            publishCashierCashbackMcResponse.setHeader(publishCashierCashbackMcRequest.getHeader());
            publishCashierCashbackMcResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                publishCashierCashbackMcResponse.setRespCode(((BizException) e).getCode());
                publishCashierCashbackMcResponse.setRespDesc(e.getMessage());
            } else {
                publishCashierCashbackMcResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                publishCashierCashbackMcResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return publishCashierCashbackMcResponseResult;
    }

    @Override
    public Result<PublishCouponMcResponse> publishCouponMc(PublishCouponMcRequest publishCouponMcRequest) {

        DubboService<PublishCouponMcRequest> dubboService = new DubboServiceImpl();
        Result<PublishCouponMcResponse> publishCouponMcResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.PUBLISH_CP_MC.getTxnNum().equals(publishCouponMcRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<PublishCouponMcResponse> dubboResult = dubboService.callAtomService(publishCouponMcRequest,
                    MompTxnNumEnum.PUBLISH_CP_MC.getService(), PublishCouponMcResponse.class);
            publishCouponMcResponseResult.setResult(dubboResult.getResult());
            publishCouponMcResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            PublishCouponMcResponse publishCouponMcResponse = new PublishCouponMcResponse();
            publishCouponMcResponseResult.setResult(publishCouponMcResponse);
            publishCouponMcResponse.setHeader(publishCouponMcRequest.getHeader());
            publishCouponMcResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                publishCouponMcResponse.setRespCode(((BizException) e).getCode());
                publishCouponMcResponse.setRespDesc(e.getMessage());
            } else {
                publishCouponMcResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                publishCouponMcResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return publishCouponMcResponseResult;
    }

    @Override
    public Result<PublishMchntFeeDiscountMcResponse> publishMchntFeeDiscountMc(PublishMchntFeeDiscountMcRequest publishMchntFeeDiscountMcRequest) {

        DubboService<PublishMchntFeeDiscountMcRequest> dubboService = new DubboServiceImpl();
        Result<PublishMchntFeeDiscountMcResponse> publishMchntFeeDiscountMcResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.PUBLISH_MFD_MC.getTxnNum().equals(publishMchntFeeDiscountMcRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<PublishMchntFeeDiscountMcResponse> dubboResult = dubboService.callAtomService(publishMchntFeeDiscountMcRequest,
                    MompTxnNumEnum.PUBLISH_MFD_MC.getService(), PublishMchntFeeDiscountMcResponse.class);
            publishMchntFeeDiscountMcResponseResult.setResult(dubboResult.getResult());
            publishMchntFeeDiscountMcResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            PublishMchntFeeDiscountMcResponse publishMchntFeeDiscountMcResponse = new PublishMchntFeeDiscountMcResponse();
            publishMchntFeeDiscountMcResponseResult.setResult(publishMchntFeeDiscountMcResponse);
            publishMchntFeeDiscountMcResponse.setHeader(publishMchntFeeDiscountMcRequest.getHeader());
            publishMchntFeeDiscountMcResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                publishMchntFeeDiscountMcResponse.setRespCode(((BizException) e).getCode());
                publishMchntFeeDiscountMcResponse.setRespDesc(e.getMessage());
            } else {
                publishMchntFeeDiscountMcResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                publishMchntFeeDiscountMcResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return publishMchntFeeDiscountMcResponseResult;
    }

    @Override
    public Result<PublishMoneyOffMcResponse> publishMoneyOffMc(PublishMoneyOffMcRequest publishMoneyOffMcRequest) {

        DubboService<PublishMoneyOffMcRequest> dubboService = new DubboServiceImpl();
        Result<PublishMoneyOffMcResponse> publishMoneyOffMcResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.PUBLISH_MO_MC.getTxnNum().equals(publishMoneyOffMcRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<PublishMoneyOffMcResponse> dubboResult = dubboService.callAtomService(publishMoneyOffMcRequest,
                    MompTxnNumEnum.PUBLISH_MO_MC.getService(), PublishMoneyOffMcResponse.class);
            publishMoneyOffMcResponseResult.setResult(dubboResult.getResult());
            publishMoneyOffMcResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            PublishMoneyOffMcResponse publishMoneyOffMcResponse = new PublishMoneyOffMcResponse();
            publishMoneyOffMcResponseResult.setResult(publishMoneyOffMcResponse);
            publishMoneyOffMcResponse.setHeader(publishMoneyOffMcRequest.getHeader());
            publishMoneyOffMcResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                publishMoneyOffMcResponse.setRespCode(((BizException) e).getCode());
                publishMoneyOffMcResponse.setRespDesc(e.getMessage());
            } else {
                publishMoneyOffMcResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                publishMoneyOffMcResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return publishMoneyOffMcResponseResult;
    }
}
